package com.ckf.login_wx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Shopper;
import com.ckf.login_wx.entity.User;
import com.ckf.login_wx.servic.ShopperService;
import com.ckf.login_wx.servic.UserService;
import com.ckf.login_wx.util.Idname;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class UserController {

    @Autowired
    private UserService userService;
	@Autowired
    private ShopperService ShopperService;


    @PostMapping("/addshopper")
    public Object addshopper(Integer id,String name,String phone,String address,String cid,String faf){
    	String stu="";
        Shopper shopper=new Shopper();
        shopper.setName(name);
        shopper.setPhone(phone);
        shopper.setAddress(address);
        shopper.setCid(cid);
		if(faf.equals("add")){
	        if (ShopperService.save(shopper)){
	        	stu="添加成功";
	        }else {
	        	stu="添加失败";
	        }
		}
		if(faf.equals("up")){
			shopper.setId(id);
	        if (ShopperService.updateById(shopper)){
	        	stu="修改成功";
	        }else {
	        	stu="修改失败";
	        }
		}
	    return stu;
    }
    @PostMapping("/adduser")
    public Map<String, Object> adduser(String username,String password,String truename,String phone,String quxianname){
        Map<String, Object> map = new HashMap<>();
        if(userService.list(new QueryWrapper<User>().eq("username",username)).size()!=0){
        	map.put("code",200);
            map.put("result","用户名已存在");
            return map;
        }
        User user=new User();
        user.setusername(username);
        user.setPassword(password);
        user.setTruename(truename);
        user.setPhone(phone);
        user.setQuxianname(quxianname);
        String quxian="";
        if(quxianname.equals("生产人员")){
        	quxian="0";
        }else if(quxianname.equals("企业管理员")){
        	quxian="3";
        }else if(quxianname.equals("销售员")){
        	quxian="2";	
        }else{
        	quxian="1";
        }
        user.setQuxian(quxian);
		if (userService.save(user)){
            map.put("code",200);
            map.put("result","添加成功");
            System.out.println("添加成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    @GetMapping("/userlist")
    public Object userlist(String pagenew){
        return userService.list();
    }
    @GetMapping("/shopperlist")
    public Object shopperlist(){
        return ShopperService.list();
    }
    @GetMapping("/userlistall")
    public Object userlistall(){
    	System.out.println(userService.list().size()+"***");
        return userService.list();
    }
    @PostMapping("/uppassword")
    public Object uppassword(String oldpwd,String newpwd,String name){
    	Map<String, Object> map = new HashMap<>();
    	QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put("UserName", name);
        mapuser.put("Password",oldpwd);
        queryWrapper.allEq(mapuser);
        User a=userService.getOne(queryWrapper);
        if (a!=null||oldpwd.equals("修改用户密码")){
        	
        	mapuser.clear();
//        	QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
//            mapuser.put("UserName", name);
//            mapuser.put("Password",newpwd);
//            queryWrapper2.allEq(mapuser);
            User u=new User();
            u.setId(a.getId());
            u.setusername(name);
            u.setPassword(newpwd);
        	if(userService.updateById(u)){
                System.out.println("密码修改成功");
                map.put("result","密码修改成功");
                map.put("code",200);
        	}else{
                map.put("result","updateById返回false");
                map.put("code",200);
        	}
        }else {
        	System.out.println(name+"****"+oldpwd);
            map.put("result","密码错误");
            map.put("code",200);
        }
        return map;
    }

    @GetMapping("/deleuserbyid")
    public boolean deleuserbyid(Integer id){
		if(userService.getById(id).getUsername().equals("admin")){
	    	return false;
		}
        return userService.removeById(id);
    }
    @GetMapping("/deleshopperbyid")
    public Object deleshopperbyid(Integer id){
        return ShopperService.removeById(id);
    }
    @GetMapping("/user/search")
    public Object usersearch(String talk,String search){
        return userService.list(new QueryWrapper<User>().like(Idname.getname(search), talk));
    }
    @GetMapping("/shopper/search")
    public Object shoppersearch(String talk){
        return ShopperService.list(new QueryWrapper<Shopper>().like("name", talk));
    }
    @GetMapping("/orduserbysearch")
    public Object orduserbysearch(String search,String talk,String pagenew){//
        Integer i = Integer.parseInt(pagenew);
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return userService.selectlike(talk,Idname.getname(search));
    }
    @GetMapping("/orduserbysearchall")
    public Object ordsellbysearchall(String search,String talk){//
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
        return userService.selectlike(talk,Idname.getname(search));
    }
    @GetMapping("/seleuserbyid")
    public Object seleuserbyid(int id){
        return userService.getById(id);
    }
    @GetMapping("/seleshopbyid")
    public Object seleshopbyid(String id){
    	Integer idint = Integer.parseInt(id);
        return ShopperService.getById(idint);
    }
    @PostMapping("/upuserbyid")
    public Object upuserbyid(int id,String username,String password,String truename,String phone,String quxianname){
    	Map<String, Object> map = new HashMap<>();
    	if(userService.list(new QueryWrapper<User>().eq("username",username)).size()!=0){
        	map.put("code",200);
            map.put("result","用户名已存在");
            return map;
        }
        User user=new User();
        user.setId(id);
        user.setusername(username);
        user.setPassword(password);
        user.setTruename(truename);
        user.setPhone(phone);
        user.setQuxianname(quxianname);
        String quxian="";
        if(quxianname.equals("生产人员")){
        	quxian="0";
        }else if(quxianname.equals("企业管理员")){
        	quxian="3";
        }else if(quxianname.equals("销售员")){
        	quxian="2";	
        }else{
        	quxian="1";
        }
        user.setQuxian(quxian);
		if (userService.updateById(user)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    @RequestMapping("/seleusernameall")
    public Object seleusernameall(){
    	userService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<userService.list().size();i++){
    		if(userService.list().get(i).getQuxian().equals("2")||userService.list().get(i).getQuxian().equals("3"))
    		s.add(userService.list().get(i).getUsername());
    	}
    	
        return s;
    }
    @RequestMapping("/selenameall")
    public Object selenameall(){
    	userService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<userService.list().size();i++){
    		if(userService.list().get(i).getQuxian().equals("0"))
    		s.add(userService.list().get(i).getTruename());
    	}
    	
        return s;
    }

    //查询所有销售人员
    @RequestMapping("/seleallanq")
    public Object seleallanq(HttpServletRequest request){
    	HttpSession session= request.getSession();
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
    	Map<String, Object> map = new HashMap<>();
    	List<User> user =userService.list();
    	List<String> s =new ArrayList<String>();
        if(quxian.equals("2")){
    		s.add(truename);
            map.put("count",s.size());
            map.put("data",s);
            return map;
        }
    	for(int i=0;i<user.size();i++){
    		if(user.get(i).getQuxian().equals("2"))
    		s.add(user.get(i).getTruename());
    	}
        map.put("count",s.size());
        map.put("data",s);
        return map;
    }

    //查询所有客户
    @RequestMapping("/seleallshopper")
    public Object seleallshopper(){
    	List<Shopper> Shopper =ShopperService.list();
    	List<String> name =new ArrayList<String>();
    	for(int i=0;i<Shopper.size();i++){
    		name.add(Shopper.get(i).getPhone());
    	}
    	Map<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("count",Shopper.size());
        map.put("data",Shopper);
        return map;
    }
    //通过电话获取名字地址
    @RequestMapping("/seleshopperbyphone")
    public Object seleshopperbyphone(String phone){
    	Map<String, Object> map = new HashMap<>();
    	List<Shopper> s=ShopperService.list(new QueryWrapper<Shopper>().eq("phone",phone));
        map.put("name2",s.get(0).getName());
        map.put("site",s.get(0).getAddress());
    	return map;
    }
}
